package com.mogist.hqc.module.order.ui

import android.content.Intent
import android.os.Bundle
import android.support.design.widget.TabLayout
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v4.app.FragmentTransaction
import com.mogist.hqc.R
import com.mogist.hqc.base.BaseMVPActivity
import com.mogist.hqc.module.order.contract.OrderListContract
import com.mogist.hqc.module.order.presenter.OrderListPresenter
import kotlinx.android.synthetic.main.activity_order_list.*
import kotlinx.android.synthetic.main.include_title.*
import talex.zsw.basecore.util.DataTool

/**
 * 作用：订单列表
 * 作者：赵小白 email:vvtale@gmail.com  
 * 修改人：
 * 修改时间：
 * 修改备注：
 */
class OrderListActivity : BaseMVPActivity<OrderListContract.Presenter>(), OrderListContract.View
{
    var pos = 0
    override fun initArgs(intent: Intent)
    {
        pos = intent.getIntExtra("pos", 0)
    }

    override fun initView()
    {
        setContentView(R.layout.activity_order_list)
        mPresenter = OrderListPresenter(this)
    }

    override fun initData()
    {
        mTitleBar.setTitle("订单")
        initTabLayout()
    }

    // --------------- TabLayout ---------------
    private val fragmentList = ArrayList<Fragment?>()
    private val titleList = ArrayList<String>()
    private val titles =
            arrayOf(DataTool.getString(R.string.order_all),
                    DataTool.getString(R.string.order_unpay),
                    DataTool.getString(R.string.order_unplay),
                    DataTool.getString(R.string.order_uncomment))
    private var mAdapter: TabFragmentAdapter? = null

    private fun initTabLayout()
    {
        //初始化标题栏数据和fragmentList
        for (title in titles)
        {
            titleList.add(title)
            fragmentList.add(null)
            mTabLayout.addTab(mTabLayout.newTab().setText(title))
        }
        mTabLayout.tabMode = TabLayout.MODE_FIXED //SCROLLABLE
        //实例化适配器
        mAdapter = TabFragmentAdapter(supportFragmentManager, mTabLayout.tabCount, titleList)
        mViewPager.adapter = mAdapter
        mTabLayout.setupWithViewPager(mViewPager) //必须在viewPager绑定适配器之后调用
        mTabLayout.setTabsFromPagerAdapter(mAdapter)
        mViewPager.offscreenPageLimit = titleList.size - 1
        mViewPager.currentItem = pos
    }

    /** 自定义ViewPager适配器  */
    private inner class TabFragmentAdapter //构造函数，Fragment管理工具manger，标题栏个数nNumTab，标题栏文本List
    (private val manager: FragmentManager, private val tabNum: Int, private val titles: ArrayList<String>) : FragmentPagerAdapter(
            manager)
    {
        override fun getItem(position: Int): Fragment
        {
            //隐藏所有fragment
            hideFragments(manager.beginTransaction())
            if (fragmentList[position] == null)
            {
                val fragment = OrderFragment()
                val bundle = Bundle()
                bundle.putInt("position", position)
                fragment.arguments = bundle
                fragmentList[position] = fragment
            }
            return fragmentList[position]!!
        }

        override fun getCount(): Int = tabNum

        /** 设置标题栏文本，必须复写  */
        override fun getPageTitle(position: Int): CharSequence = titles[position]
    }

    /** 隐藏所有fragment，提高复用率  */
    fun hideFragments(transaction: FragmentTransaction)
    {
        fragmentList.filterNotNull()
                .forEach { transaction.hide(it) }
    }

    /** 获取当前选中的Tab  */
    fun getCurr(): Int = mViewPager.currentItem


}